#!/usr/bin/perl -w
use strict;
use Time::HiRes q/gettimeofday/;
use POSIX q/strftime/;


my $infq_h;
my $outfq1_h;
my $outfq2_h;
my $config_fh;

#my $FA_path="/bluearc/data/schatz/hadoop/chr22_org/chr22.fa";
my $FA_path="chr22.fa";
my $bwa_exe="/bluearc/data/schatz/software/bin/bwa";
my $samtools="/bluearc/data/schatz/software/bin/samtools";

my $outf1_n_ = "r1.fq";
my $outf2_n_ = "r2.fq";

my $aln_ext = ".sai";
my $out_bam = "out.bam";
my $out_sam = "out.sam";
my $sorted_bam_name ="out.prefix";

my ($seconds, $microseconds) = gettimeofday;
my $counter = strftime( q/%Y%m%dT%H%M%S/, localtime($seconds)),
     sprintf("%05d", $microseconds/10);

my $out_sam_counter = $counter;
my $bwa = $bwa_exe;

my $outfq1_fin;
my $outfq2_fin;

$outfq1_fin = $counter.$outf1_n_;
$outfq2_fin = $counter.$outf2_n_;

#print STDERR "outfq1_fin: $outfq1_fin\n";
#print STDERR "outfq2_fin: $outfq2_fin\n";

open $outfq1_h,">".$outfq1_fin or die "Could not open $outfq1_fin for write $!";
open $outfq2_h,">".$outfq2_fin or die "Could not open $outfq2_fin for write $!";

while(<>){
	my $line = $_;
	chomp($line);
	if($line =~ /(.*?)(\s)(.*)/){
		my $readname = $1;
        	my $rest_of_line = $3;

		my $temp1 = $1."/1"."\n";
		my $temp2 = $1."/2"."\n";

	        $rest_of_line =~ /([ATGCN]+)(\s+)(.*)(\s+)([ATGCN]+)(\s+)(.*)/;
        
		$temp1 .= $1."\n";
		$temp1 .= "+\n";
		$temp1 .= $3."\n";

                $temp2 .= $5."\n";
                $temp2 .= "+\n";
                $temp2 .= $7."\n";

		print $outfq1_h $temp1;
		print $outfq2_h $temp2;
	}else{
		die("die damnit die");
	}
}

close $outfq1_h;
close $outfq2_h;

#print STDERR "aln cmd 1: $bwa aln $FA_path $outfq1_fin  $outfq1_fin$aln_ext\n";
system ("$bwa aln $FA_path $outfq1_fin > $outfq1_fin$aln_ext") == 0 or die"command failed at $_";

#print STDERR "2-------------\n";

#print STDERR "aln cmd 2: $bwa aln $FA_path $outfq2_fin  $outfq2_fin$aln_ext\n";
system("$bwa aln $FA_path $outfq2_fin > $outfq2_fin$aln_ext") == 0 or die"command failed at $_";
#print STDERR "3-------------\n";

my $out_sam_fin = $out_sam_counter.$out_sam;
my $outbampath = $out_sam_counter.$out_bam;
my $sortedbampath = $out_sam_counter.$sorted_bam_name;
my $pileup_fname = $out_sam_counter.".pileup";

#=cut
#print STDERR "sampe cmd: $bwa sampe $FA_path $outfq1_fin$aln_ext $outfq2_fin$aln_ext $outfq1_fin $outfq2_fin  $out_sam_fin\n";
system("$bwa sampe $FA_path $outfq1_fin$aln_ext $outfq2_fin$aln_ext $outfq1_fin $outfq2_fin  > $out_sam_fin") == 0 or die"command failed at $_";

my $outsam_fh;
open $outsam_fh,$out_sam_fin;
my $outsam_str = "";
my $key;
my $lines_read = 0;

while(<$outsam_fh>){
	chomp;
	if($lines_read eq 0){
		$key = $_;
		$lines_read++;
	}else{
		print $key."\t".$_."\n";
	}
#	$outsam_str .= $_;
#	$outsam_str .= "NLN";
}

#my @contents_outsam = <$outsam_fh>;
#my $outsam_str = join('',@contents_outsam);

#$outsam_str =~ s/\n/\[NEWLINE\]/g;
#$outsam_str .= "\n";
#print STDERR length($outsam_str);
#print $outsam_str; 
#close $outsam_fh;

print STDERR "END_OF wrap_align.pl \n";

#print STDERR "4-------------\n";
=cut
#print STDERR "view cmd: $samtools view -bSu $out_sam_fin > $outbampath\n";
system("$samtools view -bSu $out_sam_fin > $outbampath") == 0 or die "could not execute at $!";

#print STDERR "sort cmd: $samtools sort $outbampath $sortedbampath\n";
system("$samtools sort $outbampath $sortedbampath") == 0 or die "could not execute at $!";

$sortedbampath .= ".bam";
#print STDERR "index: $samtools index $sortedbampath\n";
system("$samtools index $sortedbampath") == 0 or die "could not execute at $!";

#print STDERR "mpileup cmd: $samtools mpileup -f $FA_path $sortedbampath > $pileup_fname\n";
system("$samtools mpileup -f $FA_path $sortedbampath > $pileup_fname") == 0 or die "could not execute at $!";

my $lsop = `ls -l`;
#print STDERR $lsop,"\n";
#system ("rm -rf $tmp_file_path*");
#system ("rm -rf $tmp_file_path");
#system("rm -rf $hdfspath");
#=cut
open pileup_fh,$pileup_fname;
while(<pileup_fh>){
	print $_;
}
close pileup_fh;

#print STDERR "6-------------\n";
=cut


